package com.dlmz.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;

@SuppressWarnings("serial")
public class MedicalAddDialog extends JDialog implements ActionListener{
	//=========面板控件  
    //......左侧标题栏  
    private JLabel mnumLab,snoLab,dnameLab,mdateLab,diagosisLab,mrecordLab;  
    //......右侧信息选择填写栏  
    private JTextField mnumTxt,snoTxt,dnameTxt,mdateTxt,diagosisTxt,mrecordTxt;
    //......添加和取消按钮  
    private JButton addBtn,cancelBtn;  
    //......布局控件  
    private JPanel left,center,bottom;  
	public MedicalAddDialog(JFrame owner, String title, boolean modal) {
		// TODO Auto-generated constructor stub
		//========重写父类方法  
        super(owner, title, modal);  
        //========左侧标签栏  
        mnumLab = new JLabel("病历单号: ");  
        snoLab = new JLabel("学号: "); 
        dnameLab = new JLabel("医生: ");
        mdateLab = new JLabel("病历日期: ");  
        diagosisLab = new JLabel("诊断结果: ");  
        mrecordLab = new JLabel("治疗意见: ");  
        //========右侧信息填写栏  
        mnumTxt = new JTextField();  
        snoTxt = new JTextField();  
        dnameTxt = new JTextField();  
        mdateTxt = new JTextField();  
        diagosisTxt = new JTextField();  
        mrecordTxt = new JTextField();
        //========添加和取消按钮  
        addBtn = new JButton("添加");  
        cancelBtn = new JButton("取消");  
        //......添加监听  
        addBtn.addActionListener(this);  
        addBtn.setActionCommand("add");  
        cancelBtn.addActionListener(this);  
        cancelBtn.setActionCommand("cancel");  
        //========创建布局  
        //......创建左边栏  
        left = new JPanel();  
        left.setLayout(new GridLayout(7, 1));  
        left.add(mnumLab);  left.add(snoLab);   
        left.add(dnameLab); left.add(mdateLab);   
        left.add(diagosisLab);  left.add(mrecordLab);
        //......创建右边栏  
        center = new JPanel();  
        center.setLayout(new GridLayout(7, 1));  
        center.add(mnumTxt);  center.add(snoTxt);  
        center.add(dnameTxt); center.add(mdateTxt);  
        center.add(diagosisTxt);  center.add(mrecordTxt); 
        //========底层添加和取消按钮  
        bottom = new JPanel();  
        bottom.add(addBtn);  
        bottom.add(cancelBtn);  
        //========整体布局  
        this.add(left,BorderLayout.WEST);  
        this.add(center,BorderLayout.CENTER);  
        this.add(bottom,BorderLayout.SOUTH);  
        //========设置窗口属性  
          
        validate();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(450,300);
		this.setLocationRelativeTo(null);
		setVisible(true);   
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("add")) {  
            String sql = "insert into medicalchart values(?,?,?,?,?,?)";  
            String mnum =mnumTxt.getText();
            int sno=Integer.parseInt(snoTxt.getText());
            String dname=dnameTxt.getText();
            String mdate=mdateTxt.getText();
            String diagosis=diagosisTxt.getText();
            String mrecord=mrecordTxt.getText();
            BaseDao basedao=new BaseDao();
			int num=basedao.update(sql, mnum,sno,dname,mdate,diagosis,mrecord);
			if(num>0) {
				JOptionPane.showMessageDialog(null, "添加病历信息成功！");
				this.dispose(); 
			}else {
				JOptionPane.showMessageDialog(null, "添加病历信息失败！");
			}
            //========关闭窗口  
            this.dispose();  
        } else if(e.getActionCommand().equals("cancel")) {  
            //========关闭窗口  
            this.dispose();  
        }  
	}

}
